T = 3;
tic;
market_code = 'VNINDEX';
number_of_stocks = 20;
stock_codes = []; %{'BBT','TRI','NVN','VIS','SAV','PAC','TDH','TTF','VPH'}; 
porfolio_weights = [];

alpha = 0.05;
V0 = 1;

options = {'OLS'; 'Med Robust'; 'Trim Robust'; 'Weight Robust'; 'Shrinkage'; ...
          'Bayes'; ...
          'Smoothing'; ...
          'Kalman Filter';
          'Tobit';
          'Truncated'; 'GMM'};

temp = zeros(length(options), 1);
TotalPayoff1 = temp;
FailureRate = temp;

Beta_All = temp;
Var_Up_All = temp;
Var_Down_All = temp;

beta_all = temp;
var_up_all = temp;
var_down_all = temp;

TotalPayoff2 = temp;

for t = 1:T      
    t
%     [portfolio_codes porfolio_weights portfolio_date portfolio_close_price portfolio_volume] ...
%         = getPortfolio(market_code, number_of_stocks, stock_codes, porfolio_weights);
    [portfolio_codes porfolio_weights portfolio_date portfolio_close_price portfolio_volume] = loadPortfolio(market_code, number_of_stocks, t);
    
    Stock_Price = portfolio_close_price(:, 2:end);
    Index_Price = portfolio_close_price(:, 1);
    
    %get return of Stocks and Index
    rPortfolio = getReturn(Stock_Price);
    rIndex = getReturn(Index_Price);

    % Strategy 1
    
    strategy = 1;

    j=0;
    totalPayoff1 = temp;
    totalPayoff2 = temp;
    failure_rate = temp;
    for i=1:length(options)
        options{i}
        [totalPayoff1(i) Buy1 Sell1 totalPayoff2(i) Buy2 Sell2 beta VaRup VaRdown failure_rate(i)] ...
            = trading(Stock_Price, Index_Price, rPortfolio, rIndex, options{i},...
                      porfolio_weights, alpha, V0);
        if (length(beta) == 1)
            %j = j+1;
            beta_all(i) = beta;
            var_up_all(i) = VaRup;
            var_down_all(i) = VaRdown;
        end
        if (strcmp(options{i},'Smoothing'))
            beta_smoothing = beta;
            var_smoothing = VaRdown;
            beta_all(i) = mean(beta_smoothing);
            var_down_all(i) = mean(var_smoothing);
        end
        if (strcmp(options{i},'Kalman Filter'))
            beta_kalman = beta;
            var_kalman = VaRdown;
            beta_all(i) = mean(beta_kalman);
            var_down_all(i) = mean(var_kalman);
        end
    end
    TotalPayoff1 = TotalPayoff1 + totalPayoff;
    FailureRate = FailureRate + failure_rate;
    
%     beta_all(7:8) =  [mean(beta_smoothing) mean(beta_kalman)];
%     var_down_all(7:8) = [mean(var_smoothing) mean(var_kalman)];
    
    Beta_All = Beta_All + beta_all;
    Var_Up_All = Var_Up_All + var_up_all;
    Var_Down_All = Var_Down_All + var_down_all;
    
    %Beta_Smoothing(t) = mean(beta_smoothing);
    %Var_Smoothing(t) = mean(var_smoothing);
    %Beta_Kalman(t) = mean(beta_kalman);
    %Var_Kalman(t) = mean(var_kalman);
    
    % Strategy 2
    
%     strategy = 2;
%     
%     totalPayoff = temp;
%     for i=1:length(options)
%         options{i}
%         [totalPayoff(i) Buy Sell] = trading(Stock_Price, Index_Price, rPortfolio, rIndex, options{i},...
%                                      strategy, porfolio_weights, alpha, V0);
%     end
    TotalPayoff2 = TotalPayoff2 + totalPayoff;
end
    
TotalPayoff1 = TotalPayoff1 / T;
FailureRate = FailureRate / T;
Beta_All = Beta_All / T;
Var_Up_All = Var_Up_All / T;
Var_Down_All = Var_Down_All / T;

% Beta_Smoothing = mean(Beta_Smoothing);
% Var_Smoothing = mean(Var_Smoothing);
% Beta_Kalman = mean(Beta_Kalman);
% Var_Kalman = mean(Var_Kalman);

% Beta_All = [Beta_All Beta_Smoothing Beta_Kalman];
% Var_Down_All = [Var_Down_All Var_Smoothing Var_Kalman];

TotalPayoff2 = TotalPayoff2 / T;


figure('name','Total Payoff - Strategy 1');
plot(TotalPayoff1,'g');
set(gca,'Xtick',1:40,'XTickLabel',{'OLS','Med','Trim','Wei','Shri','Bay','Smo','Kal','Tob','Tru','GMM'});
figure('name','VaR Backtest - Failure rate');
plot(FailureRate,'g');
set(gca,'Xtick',1:40,'XTickLabel',{'OLS','Med','Trim','Wei','Shri','Bay','Smo','Kal','Tob','Tru','GMM'});
figure('name','VaR All')
plot(Beta_All,'r');
set(gca,'Xtick',1:40,'XTickLabel',{'OLS','Med','Trim','Wei','Shri','Bay','Smo', 'Kal','Tob','Tru','GMM'});
figure('name','VaR Down All')
%plot(var_up_all,'g');
%hold on;
plot(Var_Down_All,'r');
set(gca,'Xtick',1:40,'XTickLabel',{'OLS','Med','Trim','Wei','Shri','Bay','Smo', 'Kal','Tob','Tru','GMM'});
hold off;

figure('name','Total Payoff - Strategy 2');
plot(TotalPayoff2);
set(gca,'Xtick',1:40,'XTickLabel',{'OLS','Med','Trim','Wei','Shri','Bay','Smo','Kal','Tob','Tru','GMM'});
toc;